import argparse
from concurrent.futures import process
import json
import os
import random
import shutil
import subprocess
import time
from multiprocessing import Process, Queue
import re


bin_dir = os.path.join("/nfs/home/share/jiaxiaoyu/simpoint_checkpoint_archive/spec17-rv64gcb-O3-20m-gcc12.2.0-mix-with-special_wrf", "checkpoint-0-0-0")
print(bin_dir)
#with open(bin_dir) as f:
data = os.listdir(bin_dir)
print(data)
name_compile = re.compile(r"(.*)_(.*)_(.*)_.zstd")
#write_buffer_all= []
write_buffer_all= {}
i =0
for cpt_big_item in data :
    cpt_name_item = os.path.join(bin_dir, cpt_big_item)
    print(cpt_name_item)

    #assert(False)
    if not os.path.isdir(cpt_name_item):
        continue
    data_item = os.listdir(cpt_name_item)
    print(data_item)
    points1 = {}
    points2 = {}    
    points12 ={}
    inst_num =[0]
    #inst_num.update({"insts":"0"})
    for cpt_num in data_item:
        cpt_item = os.path.join(cpt_name_item,cpt_num)
        print(cpt_item)
        cpt = os.listdir(cpt_item)
        print(cpt)
        name1 = name_compile.match(str(cpt))
        if  name1 is not None:
            #points1.append(name1.group(2))
            #points2.append(name1.group(3))

            #points12.append((name1.group(2) + ":"+(name1.group(3))))
            points12.update({str(name1.group(2)) : str(name1.group(3))})

    print(points12)
   # assert(False)
    #print(points2)


    #write_buffer = {cpt_big_item:{"insts":inst_num,"points" : points12 }}
    write_buffer = {"insts":inst_num,"points" : points12 }
    #write_buffer_all.append(write_buffer)
    print(write_buffer)
    #write_buffer_all.update({str(cpt_big_item): write_buffer})
    write_buffer_all.update({str(cpt_big_item):write_buffer})
    if i==2:
        print(write_buffer_all)
    i = i+1
    print(write_buffer)
    #assert(False)
print(write_buffer_all)
file =open("set.json","w")
file.write(str(write_buffer_all))
file.close
write_buffer_list = list(str(write_buffer_all))
with open('json.json','w+') as f1:
   json.dump(write_buffer_all,f1,indent =4)   